package Service

import (
	"errors"
	"github.com/astaxie/beego/logs"
	"net"
)

func CloseChan(ch chan []byte) {
	defer func() {
		if recover() != nil {
			////
		}
	}()

	close(ch) // panic if ch is closed
}

func CloseConn(conn net.Conn) {
	defer func() {
		if err := recover(); err != nil {
			//logs.Debug("close conn error: ", err)
		}
	}()

	conn.Close()
}

func WriteChWithSelect(ch chan []byte, val []byte) error {
	defer func() {
		if err := recover(); err != nil {
			logs.Debug("error: %s\n", err)
		}
	}()
	if len(ch) == cap(ch) {
		<-ch
	}
	select {
	case ch <- val:
		return nil
	default:
		return errors.New("channel blocked, can not write")
	}
}
